package logic

import (
	"context"
	"shopping-mall/models/redis"
	"shopping-mall/shoppingrpc/internal/svc"
	"shopping-mall/shoppingrpc/shoppingrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductsLikeListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductsLikeListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductsLikeListLogic {
	return &ProductsLikeListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProductsLikeListLogic) ProductsLikeList(in *shoppingrpc.ProductsLikeListRequest) (*shoppingrpc.ProductsLikeListResponse, error) {
	// todo: add your logic here and delete this line

	scores := redis.Rdb.ZRevRangeWithScores("productLike", 0, 9)

	var slice []*shoppingrpc.ProductLikeList

	for _, v := range scores.Val() {
		list := shoppingrpc.ProductLikeList{
			Likes: int64(v.Score),
			Name:  v.Member.(string),
		}
		slice = append(slice, &list)
	}
	return &shoppingrpc.ProductsLikeListResponse{List: slice}, nil
}
